<?php

namespace Alipay\AlipayAuth;

use Alipay\AlipayAuth\Request\AlipayAuthISVInviteQueryRequest;
use Alipay\AlipayAuth\Request\AlipayAuthISVInviteRequest;
use Alipay\AlipayAuth\Request\AlipayAuthOauthTokenRequest;
use Alipay\AlipayAuth\Request\AlipayAuthTokenListRequest;
use Alipay\AlipayAuth\Request\AlipayAuthTokenRequest;

class AlipayAuth extends Builder
{
    protected $authGateway = 'https://openauth.alipay.com/oauth2/appToAppBatchAuth.htm';

    public function __construct(array $config)
    {
        $this->appId = $config['APPID'] ?? '';
        $this->privateKey = $config['APP_PRIVATE_KEY'] ?? '';
        $this->redirectUrl = $config['REDIRECT_URL'] ?? '';
        $this->logsDir = $config['LOGS_DIR'] ?? '';
    }

    /**
     * 获取批量授权链接地址
     * @return string
     * @author xis
     */
    public function authUrl(): string
    {
        $string = '';
        $param = [
            'app_id' => $this->appId,
            'application_type' => 'WEBAPP,MOBILEAPP',
            'redirect_uri' => urlencode($this->redirectUrl)
        ];
        foreach ($param as $key => $value) {
            $string .= $key . '=' . $value . '&';
        }

        return $this->authGateway . '?' . trim($string, '&');
    }

    /**
     * 发起授权
     * @param string $appAuthCode
     * @return AlipayAuthTokenRequest
     * @author xis
     */
    public function authToken(string $appAuthCode): AlipayAuthTokenRequest
    {
        $batchCreateRequest = new AlipayAuthTokenRequest();

        $batchCreateRequest->setAuthCode($appAuthCode);

        return $batchCreateRequest;
    }

    /**
     * 授权列表
     * @param string $appAuthToken
     * @return AlipayAuthTokenListRequest
     * @author xis
     */
    public function authTokenList(string $appAuthToken): AlipayAuthTokenListRequest
    {
        $batchCreateRequest = new AlipayAuthTokenListRequest();

        $batchCreateRequest->setAppAuthToken($appAuthToken);

        return $batchCreateRequest;
    }

    /**
     * 换取授权访问令牌
     * @param string $OauthToken
     * @return AlipayAuthOauthTokenRequest
     * @author xis
     */
    public function OauthToken(string $OauthToken): AlipayAuthOauthTokenRequest
    {
        $oauthToken = new AlipayAuthOauthTokenRequest();
        $oauthToken->setCode($OauthToken);
        return $oauthToken;
    }


        /**
     * ISV 邀约即授权页面创建
     * @author xis
     */
    public function ISVInvite(string $bizNo, string $returnUrl = ''): AlipayAuthISVInviteRequest
    {
        $inviteRequest = new AlipayAuthISVInviteRequest();
        $inviteRequest->setBizNo($bizNo);
        $inviteRequest->setReturnUrl($returnUrl ?: $this->redirectUrl);

        return $inviteRequest;
    }

    /**
     * ISV 查询签约申请单状态
     * @param string $bizNo
     * @return AlipayAuthISVInviteQueryRequest
     * @author xis
     */
    public function ISVInviteQuery(string $bizNo): AlipayAuthISVInviteQueryRequest
    {
        $queryRequest = new AlipayAuthISVInviteQueryRequest();
        $queryRequest->setBizNo($bizNo);

        return $queryRequest;
    }

}
